﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class Frm_BQYGInfo : DockContent
    {
        private DGBY.BLL.BZSortTab m_bllBZSortTab = new DGBY.BLL.BZSortTab();
        private DataSet m_dsBZSortTab = new DataSet();
        private int m_nSelectBZSortTabID = 0;
        private DGBY.BLL.QDPlaceTab m_bllQDPlaceTab = new DGBY.BLL.QDPlaceTab();
        private DataSet m_dsQDPlaceTab = new DataSet();
        private int m_nSelectQDPlaceTabID = 0;
        private DGBY.BLL.YSMethodTab m_bllYSMethodTab = new DGBY.BLL.YSMethodTab();
        private DataSet m_dsYSMethodTab = new DataSet();
        private int m_nSelectYSMethodTabID = 0;
        private DGBY.BLL.GuiXingTab m_bllGuiXingTab = new DGBY.BLL.GuiXingTab();
        private DataSet m_dsGuiXingTab = new DataSet();
        private int m_nSelectGuiXingTabID = 0;

        public Frm_BQYGInfo()
        {
            InitializeComponent();
        }

        private void ShowAllBZSortRecordInDataGridView()
        {
            dataGridViewBzCateList.DataSource = null;
            m_dsBZSortTab = m_bllBZSortTab.GetAllList();
            if (m_dsBZSortTab.Tables.Count > 0 && m_dsBZSortTab.Tables[0].Rows.Count > 0)
            {
                dataGridViewBzCateList.DataSource = m_dsBZSortTab.Tables[0];
                dataGridViewBzCateList.Columns[0].Visible = false;
            }
        }

        private void buttonBzCateShowAllRecords_Click(object sender, EventArgs e)
        {
            ShowAllBZSortRecordInDataGridView();
        }

        private void buttonAddBzCate_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BZSortTab modelBZSortTab = new DGBY.Model.BZSortTab();
                modelBZSortTab.BZSortName = this.textBoxBzCate.Text;


                int nResult = m_bllBZSortTab.Add(modelBZSortTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加包装种类", modelBZSortTab.BZSortName);

                    m_nSelectBZSortTabID = m_bllBZSortTab.GetMaxId() - 1;
                    ShowAllBZSortRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            
        }

        private void buttonModifyBzCate_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllBZSortTab.GetModel(m_nSelectBZSortTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.BZSortTab modelBZSortTab = m_bllBZSortTab.GetModel(m_nSelectBZSortTabID);
                modelBZSortTab.BZSortName = this.textBoxBzCate.Text;
                //modelBZSortTab.BZSortTabID = m_nSelectBZSortTabID;

                m_bllBZSortTab.Update(modelBZSortTab);

                Frm_Main.Log2DB(this.Text, "修改包装种类", modelBZSortTab.BZSortName);
                ShowAllBZSortRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void buttonDelBzCate_Click(object sender, EventArgs e)
        {
            if (m_bllBZSortTab.GetModel(m_nSelectBZSortTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllBZSortTab.Delete(m_nSelectBZSortTabID);
            ShowAllBZSortRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickBZSort();
        }

        private void dataGridViewBzCateList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strBZSortID = this.dataGridViewBzCateList.Rows[this.dataGridViewBzCateList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectBZSortTabID = Convert.ToInt32(strBZSortID, 10);
            cellclickBZSort();
        }

        private void cellclickBZSort()
        {
            DGBY.Model.BZSortTab modelBZSortTab = m_bllBZSortTab.GetModel(m_nSelectBZSortTabID);
            if (modelBZSortTab != null)
            {
                this.textBoxBzCate.Text = modelBZSortTab.BZSortName;
            }
            else
            {
                this.textBoxBzCate.Text = "";
            }
        }

        private void dataGridViewBzCateList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewBzCateList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void ShowAllRecordPlaceInDataGridView()
        {
            dataGridViewPlaceList.DataSource = null;
            m_dsQDPlaceTab = m_bllQDPlaceTab.GetAllList();
            if (m_dsQDPlaceTab.Tables.Count > 0 && m_dsQDPlaceTab.Tables[0].Rows.Count > 0)
            {
                dataGridViewPlaceList.DataSource = m_dsQDPlaceTab.Tables[0];
                dataGridViewPlaceList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecordPlace_Click(object sender, EventArgs e)
        {
            ShowAllRecordPlaceInDataGridView();
        }

        private void buttonAddPlace_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.QDPlaceTab modelQDPlaceTab = new DGBY.Model.QDPlaceTab();
                modelQDPlaceTab.QDPlaceName = this.textBoxPlace.Text;


                int nResult = m_bllQDPlaceTab.Add(modelQDPlaceTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加签订地点", modelQDPlaceTab.QDPlaceName);

                    m_nSelectQDPlaceTabID = m_bllQDPlaceTab.GetMaxId() - 1;
                    ShowAllRecordPlaceInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void dataGridViewPlaceList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strPlaceID = this.dataGridViewPlaceList.Rows[this.dataGridViewPlaceList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectQDPlaceTabID = Convert.ToInt32(strPlaceID, 10);
            cellclickPlace();
        }

        private void cellclickPlace()
        {
            DGBY.Model.QDPlaceTab modelQDPlaceTab = m_bllQDPlaceTab.GetModel(m_nSelectQDPlaceTabID);
            if (modelQDPlaceTab != null)
            {
                this.textBoxPlace.Text = modelQDPlaceTab.QDPlaceName;
            }
            else
            {
                this.textBoxPlace.Text = "";
            }
        }

        private void dataGridViewPlaceList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewPlaceList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonModifyPlace_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllQDPlaceTab.GetModel(m_nSelectQDPlaceTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.QDPlaceTab modelQDPlaceTab = m_bllQDPlaceTab.GetModel(m_nSelectQDPlaceTabID);
                modelQDPlaceTab.QDPlaceName = this.textBoxPlace.Text;
               // modelQDPlaceTab.QDPlaceTabID = m_nSelectQDPlaceTabID;

                m_bllQDPlaceTab.Update(modelQDPlaceTab);

                Frm_Main.Log2DB(this.Text, "修改签订地点", modelQDPlaceTab.QDPlaceName);
                ShowAllRecordPlaceInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonDelPlace_Click(object sender, EventArgs e)
        {
            if (m_bllQDPlaceTab.GetModel(m_nSelectQDPlaceTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllQDPlaceTab.Delete(m_nSelectQDPlaceTabID);
            ShowAllRecordPlaceInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickPlace();
        }

        private void ShowAllTransWayRecordInDataGridView()
        {
            dataGridViewTransWay.DataSource = null;
            m_dsYSMethodTab = m_bllYSMethodTab.GetAllList();
            if (m_dsYSMethodTab.Tables.Count > 0 && m_dsYSMethodTab.Tables[0].Rows.Count > 0)
            {
                dataGridViewTransWay.DataSource = m_dsYSMethodTab.Tables[0];
                dataGridViewTransWay.Columns[0].Visible = false;
            }
        }

        private void buttonTransWayShowAll_Click(object sender, EventArgs e)
        {
            ShowAllTransWayRecordInDataGridView();
        }

        private void dataGridViewTransWay_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strTransWayID = this.dataGridViewTransWay.Rows[this.dataGridViewTransWay.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectYSMethodTabID = Convert.ToInt32(strTransWayID, 10);
            cellclickTransWay();
        }

        private void cellclickTransWay()
        {
            DGBY.Model.YSMethodTab modelYSMethodTab = m_bllYSMethodTab.GetModel(m_nSelectYSMethodTabID);
            if (modelYSMethodTab != null)
            {
                this.textBoxTransWay.Text = modelYSMethodTab.YSMethodName;
            }
            else
            {
                this.textBoxTransWay.Text = "";
            }
        }

        private void dataGridViewTransWay_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewTransWay.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAddTransWay_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.YSMethodTab modelYSMethodTab = new DGBY.Model.YSMethodTab();
                modelYSMethodTab.YSMethodName = this.textBoxTransWay.Text;


                int nResult = m_bllYSMethodTab.Add(modelYSMethodTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加运输方式", modelYSMethodTab.YSMethodName);

                    m_nSelectYSMethodTabID = m_bllYSMethodTab.GetMaxId() - 1;
                    ShowAllTransWayRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonModTransWay_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllYSMethodTab.GetModel(m_nSelectYSMethodTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.YSMethodTab modelYSMethodTab = m_bllYSMethodTab.GetModel(m_nSelectYSMethodTabID);
                modelYSMethodTab.YSMethodName = this.textBoxTransWay.Text;
                //modelYSMethodTab.YSMethodTabID = m_nSelectYSMethodTabID;

                m_bllYSMethodTab.Update(modelYSMethodTab);

                Frm_Main.Log2DB(this.Text, "修改运输方式", modelYSMethodTab.YSMethodName);
                ShowAllTransWayRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonDelTransWay_Click(object sender, EventArgs e)
        {
            DGBY.Model.YSMethodTab modelYSMethodOldTab = m_bllYSMethodTab.GetModel(m_nSelectYSMethodTabID);
            if (m_bllYSMethodTab.GetModel(m_nSelectYSMethodTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllYSMethodTab.Delete(m_nSelectYSMethodTabID);
            Frm_Main.Log2DB(this.Text, "删除运输方式", modelYSMethodOldTab.YSMethodName);
            ShowAllTransWayRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickTransWay();
        }

        private void ShowAllGuiXingRecordInDataGridView()
        {
            dataGridViewGuiXingList.DataSource = null;
            m_dsGuiXingTab = m_bllGuiXingTab.GetAllList();
            if (m_dsGuiXingTab.Tables.Count > 0 && m_dsGuiXingTab.Tables[0].Rows.Count > 0)
            {
                dataGridViewGuiXingList.DataSource = m_dsGuiXingTab.Tables[0];
                dataGridViewGuiXingList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecordGuiXing_Click(object sender, EventArgs e)
        {
            ShowAllGuiXingRecordInDataGridView();
        }

        private void dataGridViewGuiXingList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strGuiXingID = this.dataGridViewGuiXingList.Rows[this.dataGridViewGuiXingList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectGuiXingTabID = Convert.ToInt32(strGuiXingID, 10);
            cellclickGuiXing();
        }

        private void cellclickGuiXing()
        {
            DGBY.Model.GuiXingTab modelGuiXingTab = m_bllGuiXingTab.GetModel(m_nSelectGuiXingTabID);
            if (modelGuiXingTab != null)
            {
                this.textBoxGuiXing.Text = modelGuiXingTab.GuiXingName;
                this.textBoxGuiXingType.Text = modelGuiXingTab.GuiXingTypeName;
            }
            else
            {
                this.textBoxGuiXing.Text = "";
                this.textBoxGuiXingType.Text = "";
            }
        }

        private void dataGridViewGuiXingList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewGuiXingList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAddGuiXing_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.GuiXingTab modelGuiXingTab = new DGBY.Model.GuiXingTab();
                modelGuiXingTab.GuiXingName = this.textBoxGuiXing.Text;
                modelGuiXingTab.GuiXingTypeName = this.textBoxGuiXingType.Text;

                int nResult = m_bllGuiXingTab.Add(modelGuiXingTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加柜型", modelGuiXingTab.GuiXingName);

                    m_nSelectGuiXingTabID = m_bllGuiXingTab.GetMaxId() - 1;
                    ShowAllGuiXingRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
         
        }

        private void buttonModifyGuiXing_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllGuiXingTab.GetModel(m_nSelectGuiXingTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.GuiXingTab modelGuiXingTab = m_bllGuiXingTab.GetModel(m_nSelectGuiXingTabID);
                modelGuiXingTab.GuiXingName = this.textBoxGuiXing.Text;
                modelGuiXingTab.GuiXingTypeName = this.textBoxGuiXingType.Text;
                //modelGuiXingTab.GuiXingTabID = m_nSelectGuiXingTabID;

                m_bllGuiXingTab.Update(modelGuiXingTab);

                Frm_Main.Log2DB(this.Text, "修改柜型", modelGuiXingTab.GuiXingName);
                ShowAllGuiXingRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonDelGuiXing_Click(object sender, EventArgs e)
        {
            if (m_bllGuiXingTab.GetModel(m_nSelectGuiXingTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllGuiXingTab.Delete(m_nSelectGuiXingTabID);
            ShowAllGuiXingRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickGuiXing();
        }

        private void Frm_BQYGInfo_Load(object sender, EventArgs e)
        {
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDelBzCate.Visible = true;
                this.buttonDelPlace.Visible = true;
                this.buttonDelTransWay.Visible = true;
                this.buttonDelGuiXing.Visible = true;
            }
        }
    }
}